<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class TestController extends Controller
{
    public function index(Request $request)
    {
        $state = $request->input('state');
        // yqlog(urldecode($state));
        yqlog($request->all());
        return yqok();
    }

    public function ttlogin(Request $request)
    {
        $url = 'https://developer.toutiao.com/api/apps/v2/jscode2session';
        $appid = 'ttf7f5814868ca061c01';
        $secret = '1aa4286905a50586fb3358d864d43a3fb9781e36';

        $response = Http::post($url, [
            'appid' => $appid,
            'secret' => $secret,
            'code' => $request->input('code'),
            'anonymous_code' => '',
        ]);

        $data = json_decode($response->body(), true);
        return yqok([
            'openid' => $data['data']['openid'],
            'unionid' => $data['data']['unionid'],
        ]);
    }
}
